class CommentsController < ApplicationController

  before_filter :set_comment, :only => [:show,:edit, :update, :destroy]
  before_filter :authorize_student, :only => [:new, :create]
  before_filter :authorize_comment_owner, :only => [:edit, :update, :destroy]
 
  # list all actions which needs setting @commentable variable
  before_filter :set_commentable, :only => [:list, :new, :destroy, :reveal_comments ,:hide_comments, :hide_comment_btn ,:reveal_comment_btn ]
   
  #generates form to new comment
  def new
    @comment = Comment.new
    render :partial => "comments/new"
  end

  #creates new comment
  def create
    @comment = Comment.new(params[:comment])
    @comment.user = session[:user]
    @comment.content = @comment.content
    @commentable = @comment.commentable
    if @comment.save
      flash[:notice] = 'Comment was successfully created.'
      @comments = @commentable.last_comments
      @comments_size = @commentable.comments.size.to_s
      render :partial => "comments/list"
    else
      render :partial => "comments/new"
    end
  end

  #shows one comment
  def show
    @commentable = @comment.commentable
    render :partial => 'comment', :locals => {:comment => @comment}
  end
   
  #shows edit form for comment 
  def edit
    @commentable = @comment.commentable
    render :partial => "comments/edit"
  end
  
  #updates comment 
  def update
    @commentable = @comment.commentable
    @comment.content = (params[:comment][:content])
    if @comment.save
      flash[:notice] = 'Comment was successfully updated.'
      @comments = @commentable.last_comments
      render :partial => 'comment', :locals => {:comment => @comment}
    else
      render :partial => "comments/edit"
    end
  end
  
  #deletes comment 
  def destroy
    @comment.destroy if @comment 
    @comments = @commentable.last_comments
    @comments_size = @commentable.comments.size.to_s
    render :partial => "comments/list" and return
  end
   
  #hides all comments . Used after clicking on hide button  (AJAX called)
  def hide_comments
    @comments = @commentable.last_comments
    render :partial => "/comments/comment_list"
  end
  
  #reveals all comments . Used after clicking on reveal button   (AJAX called)
  def reveal_comments
    @comments_revealed = true
    @comments = @commentable.comments
    render :partial => "/comments/comment_list"
  end 
  
  #shows hide commnets button. Used after revealing comments (AJAX called)  
  def hide_comment_btn
    render :partial => "/comments/hide_btn"
  end
  
  #shows reveal commnets button. Used after hiding comments  (AJAX called) 
  def reveal_comment_btn
    render :partial => "/comments/reveal_btn"
  end
  
  def list
    @comments = @commentable.last_comments
    @comments_size = @commentable.comments.size.to_s
    render :partial => "/comments/list"
  end

  private

  def set_comment
    if Comment.exists?(params[:id]) 
      @comment = Comment.find(params[:id])
    end
  end
  
  #sets reference to comment ecase or event
  def set_commentable
    type = params[:commentable_type]
    id = params[:commentable_id]
    # does comment belong to ecase or to event
    @commentable = (type == "Ecase") ? Ecase.find(id) : Event.find(id)
  end
  
  def authorize_comment_owner
    return false unless authorize_student

    authorized = admin_authorized? || (get_session_user.id == Comment.find(@comment.id).user_id) 

    unless authorized
      on_not_authorized("Please login as author of this ecase.")
    end

    authorized
  end
end